package com.pronhub.cloud.shop.mapper;

import com.pronhub.cloud.shop.domain.Good;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.SelectProvider;

import java.util.List;

@Mapper
public interface GoodMapper {
    //select * from t_goodswhere id in (1,2)
    //foreach标签
    //注解的方式,注解方式如何实现动态sql?
    //mybatis可以使用xml或者注解方式.
    //当前这个方法的SQL是由GoodMapperSQLProvider.queryByIds返回的字符串定义
    @SelectProvider(type = GoodMapperSQLProvider.class,method = "queryByIds")
    List<Good> queryByIds(@Param("ids") List<Long> ids);

    class GoodMapperSQLProvider{
        public String queryByIds(@Param("ids")List<Long> ids){
            StringBuilder sb = new StringBuilder();
            sb.append(" select * from t_goods ");
            if(ids!=null && ids.size()>0){
                sb.append(" where id in ( ");
                for(int i=0;i<ids.size();i++){
                    if(i!=0){
                        sb.append(",");
                    }
                    sb.append(ids.get(i));
                }
                sb.append(" )");
            }
            return sb.toString();
        }
    }
}
